#include "fyp/render/TextureImpl.h"

//------------------------------------------------------------------------------
// additional header includes
#include "fyp/render/ResourceFactory.h"

#include <d3d9.h>

//------------------------------------------------------------------------------
namespace ming
{
namespace fyp
{
namespace render
{
	//--------------------------------------------------------------------------
	TTextureImpl::TTextureImpl(const char *filename, TResourceFactory *resFactory) :
		mResFactory(resFactory)
	{
		mTexture = mResFactory->LoadTexture(filename);

		SetAddressMode(WRAP, WRAP, WRAP);
		SetFilterMode(LINEAR, LINEAR, LINEAR);
	}
	
	//--------------------------------------------------------------------------
	TTextureImpl::~TTextureImpl()
	{
		if(mTexture)
			mResFactory->UnloadTexture(mTexture);
	}

	//--------------------------------------------------------------------------
	void* TTextureImpl::GetApiTexture()
	{
		return mTexture;
	}
	
	//--------------------------------------------------------------------------
	tuple<size_t, size_t> TTextureImpl::GetActualSize()
	{
		if(!mTexture)
			return core::make_tuple(size_t(0), size_t(0));

		D3DSURFACE_DESC desc;
		mTexture->GetLevelDesc(0, &desc);

		return core::make_tuple(size_t(desc.Width), size_t(desc.Height));
	}

	//--------------------------------------------------------------------------
	void TTextureImpl::SetAddressMode(
		TAddressMode u,
		TAddressMode v,
		TAddressMode w)
	{
		get<0>(mAddressModes) = u;
		get<1>(mAddressModes) = v;
		get<2>(mAddressModes) = w;
	}

	//--------------------------------------------------------------------------
	void TTextureImpl::SetFilterMode(
		TFilterMode minFilter,
		TFilterMode magFilter,
		TFilterMode mipFilter)
	{
		get<0>(mFilterModes) = minFilter;
		get<1>(mFilterModes) = magFilter;
		get<2>(mFilterModes) = mipFilter;
	}

	//--------------------------------------------------------------------------
	ITexture::TAddressModeSettings TTextureImpl::GetAddressMode()
	{
		return mAddressModes;
	}
	
	//--------------------------------------------------------------------------
	ITexture::TFilterModeSettings TTextureImpl::GetFilterMode()
	{
		return mFilterModes;
	}
	
	//--------------------------------------------------------------------------
}
}
}
